import logging
import requests
from api.login_api import Mepsking_Login
from common.utils import exe_sql_get_data, ordertoken
from config import Base_Host
class Goods:
    def add_goods(self,json_dict,token):
        url=Base_Host+'/api/product/goods/save'
        header_dict={"accesstoken":token}
        resp=requests.post(url=url,json=json_dict,headers=header_dict)
        logging.info('添加商品接口返回的数据为：{}'.format(resp.json()))
        return resp
    def audit_goods(self,json_dict,token):
        resp=self.add_goods(json_dict,token)
        if resp.json().get("code")==200:
            spuId=resp.json().get("result").get("spuId")
            url=Base_Host+'/api/product/goods/saveGoodsAudit'
            header_dict={"accesstoken":token}
            json_dict={"spuName":"","spuId":spuId,"auditStatus":"AUDIT_SUCCESS","auditReason":"","remark":""}
            resp=requests.post(url=url,json=json_dict,headers=header_dict)
            logging.info('商品审核接口返回的数据为：{}'.format(resp.json()))
            return [resp,spuId]
        else:
            logging.info('商品审核接口返回的数据为：{}'.format(resp.json()))
            return [resp,None]
    def search_goods(self,goods_name,token):
        url=Base_Host+'/api/product/goods/page?pageNo=1&pageSize=10&spuName={}&status=&optType=GOODS_MANAGE'.format(goods_name)
        header_dict={"accesstoken":token}
        resp=requests.get(url=url,headers=header_dict)
        logging.info('搜索商品接口返回的数据为：{}'.format(resp.json()))
        return resp
    def updata_goods(self,json_dict,token):
        resp=self.audit_goods(json_dict,token)
        if resp[0].json().get("code")==200:
            spuId=resp[1]
            skuId_sql="SELECT id FROM goods_sku WHERE spu_id='{}'".format(spuId)
            skuId=exe_sql_get_data(skuId_sql)
            img_id_list=[]
            for i in skuId:
                img_id_sql="SELECT id FROM goods_sku_images WHERE sku_id='{}'".format(i)
                img_id=exe_sql_get_data(img_id_sql)
                img_id_list.append(img_id)
            createBy_list=[]
            name='自动化商品编辑后的名称{}'.format(ordertoken())
            for i in skuId:
                createBy_sql = "SELECT create_by FROM goods_sku_images WHERE sku_id=''".format(i)
                createBy=exe_sql_get_data(createBy_sql)
                createBy_list.append(createBy)
            url=Base_Host+'/api/product/goods/update'
            header_dict={"accesstoken":token}
            json_dict={"deputySpuId":[],"description":"","mobileDescription":"","mobileDesc":"{}","pcDesc":"{}","draftStatus":"DRAFT_ON","goodsSpuImagesDtoList":[{"imgName":"","imgUrl":"","coverUrl":"","type":"VIDEO_LOCAL"}],"goodsSpuAttrValueRelationDtoList":[{"attrName":"颜色","attrValue":"红,黑"}],"goodsSkuDtoList":[{"id":skuId[0],"price":10,"hoverImage":"","sellPrice":10,"costPrice":0,"sellStatus":"SELL_STATUS_OFF","useStatus":"USE_ON","skuCode":"","stock":None,"weight":1,"attrs":[{"attrName":"颜色","attrValue":"红"}],
                    "images":[{"id":img_id_list[0],"createBy":createBy_list[0],"createTime":"2023-05-04 10:11:11","skuId":skuId[0],"imgUrl":"https://tmotor-dev.oss-cn-hangzhou.aliyuncs.com/material/1/微信图片_20230426163308.png","imgName":"微信图片_20230426163308","seq":0}],"signImage":"SIGN_YES"},{"id":skuId[1],"price":10,"hoverImage":"","sellPrice":10,"costPrice":0,"sellStatus":"SELL_STATUS_OFF","useStatus":"USE_ON","skuCode":"","stock":None,"weight":1,"attrs":[{"attrName":"颜色","attrValue":"黑"}],
                    "images":[{"id":img_id_list[1],"createBy":createBy_list[1],"createTime":"2023-05-04 10:11:11","skuId":skuId[1],"imgUrl":"https://tmotor-dev.oss-cn-hangzhou.aliyuncs.com/material/1/微信图片_20230426163308.png","imgName":"微信图片_20230426163308","seq":0}],"signImage":"SIGN_NO"}],"goodsSpuSeoDto":None,"otherInfoDto":None,"goodsSpuInfoDto":{"id":spuId,"spuName":name,"spuTitle":"","categoryId":"1581899050415919105"},"editOptType":"EDIT_NO"}
            resp=requests.post(url=url,json=json_dict,headers=header_dict)
            logging.info('编辑商品接口返回的数据为：{}'.format(resp.json()))
            return resp
        else:
            logging.info('编辑商品接口返回的数据为：{}'.format(resp[0].json()))
            return resp[0]
    def up_goods(self,spuId,token):
        url=Base_Host+'/api/product/goods/upGoods'
        json_dict={"id":spuId,"status":"DELETE_ON"}
        header_dict={"accesstoken":token}
        resp=requests.post(url=url,json=json_dict,headers=header_dict)
        logging.info('商品上架接口返回的数据为：{}'.format(resp.json()))
        json_dict = {"id": spuId, "status": "DELETE_OFF"}
        resp = requests.post(url=url, json=json_dict, headers=header_dict)
        logging.info('商品下架接口返回的数据为：{}'.format(resp.json()))
        return resp
    def move_goods_to_GC(self,spuId,token):
        url=Base_Host+'/api/product/goods/moveToGC'
        json_dict={"id":spuId,"deleteStatus":"DELETED"}
        header_dict={"accesstoken":token}
        resp=requests.post(url=url,json=json_dict,headers=header_dict)
        logging.info('商品移动至回收站接口返回的数据为：{}'.format(resp.json()))
        return resp
    def delete_goods(self,spuId,token):
        url=Base_Host+'/api/product/goods/updateGoodsShowStatus?id={}'.format(spuId)
        header_dict = {"accesstoken": token}
        resp = requests.post(url=url, headers=header_dict)
        logging.info('商品删除接口返回的数据为：{}'.format(resp.json()))
        return resp
    def goods_add_classify(self,json_dict,token):
        url=Base_Host+'/api/product/shopGoods/save'
        header_dict={"accesstoken":token}
        resp=requests.post(url=url,json=json_dict,headers=header_dict)
        logging.info('商品分类-添加前台类目接口返回的数据为：{}'.format(resp.json()))
        return resp
    def goods_search_classify(self,goods_class_name,token):
        url=Base_Host+'/api/product/shopGoods/tree?name={}'.format(goods_class_name)
        header_dict = {"accesstoken": token}
        resp = requests.get(url=url, headers=header_dict)
        logging.info('商品分类-搜索前台类目接口返回的数据为：{}'.format(resp.json()))
        return resp
    def goods_up_classify(self,goods_class_name,token):
        classify_id = exe_sql_get_data("SELECT id FROM goods_shop_category WHERE name='%s'"%(goods_class_name))
        json_dict={"id":classify_id[0],"name":"auto_测试编辑后的名称","spuIds":[],"treeIds":"","lvl":1,"metaKey":"","metaTitle":"","metaDesc":"","staticUrl":"auto_test"}
        url=Base_Host+'/api/product/shopGoods/update'
        header_dict = {"accesstoken": token}
        resp = requests.post(url=url, json=json_dict, headers=header_dict)
        logging.info('商品分类-编辑前台类目接口返回的数据为：{}'.format(resp.json()))
        return [resp,json_dict.get("name")]
    def goods_delete_classify(self,goods_class_name,token):
        classify_id = exe_sql_get_data("SELECT id FROM goods_shop_category WHERE name='%s'"%(goods_class_name))
        url=Base_Host+'/api/product/shopGoods/delete?id={}'.format(classify_id[0])
        header_dict = {"accesstoken": token}
        resp = requests.post(url=url, headers=header_dict)
        logging.info('商品分类-删除前台类目接口返回的数据为：{}'.format(resp.json()))
        return resp
    def goods_ht_add_classify(self,json_dict,token):
        url=Base_Host+'/api/product/category/save'
        header_dict = {"accesstoken": token}
        resp=requests.post(url=url,json=json_dict,headers=header_dict)
        logging.info('商品分类-添加后台类目接口返回的数据为：{}'.format(resp.json()))
        return resp
    def goods_ht_search_classify(self,classify_name,token):
        url=Base_Host+'/api/product/category/tree?name={}'.format(classify_name)
        header_dict = {"accesstoken": token}
        resp=requests.get(url=url,headers=header_dict)
        logging.info('商品分类-搜索后台类目接口返回的数据为：{}'.format(resp.json()))
        return resp
    def goods_ht_up_classify(self,goods_class_name,up_name,token):
        url=Base_Host+'/api/product/category/update'
        header_dict = {"accesstoken": token}
        classify_id=exe_sql_get_data("SELECT id FROM goods_category WHERE name='%s'"%(goods_class_name))
        json_dict={"id":classify_id[0],"name":up_name,"enabled":"ENABLED","type":[],"seq":1,"specJson":[],"pid":"0","lvl":1}
        resp=requests.post(url=url,json=json_dict,headers=header_dict)
        logging.info('商品分类-编辑后台类目接口返回的数据为：{}'.format(resp.json()))
        return [resp,up_name]
    def goods_ht_delete_classify(self,goods_class_name,token):
        classify_id = exe_sql_get_data("SELECT id FROM goods_category WHERE name='%s'"%(goods_class_name))
        url=Base_Host+'/api/product/category/delete?id={}'.format(classify_id[0])
        header_dict = {"accesstoken": token}
        resp=requests.post(url=url,headers=header_dict)
        logging.info('商品分类-删除后台类目接口返回的数据为：{}'.format(resp.json()))
        return resp
if __name__ == '__main__':
    test=Goods()
    login = Mepsking_Login()
    login_dict = {"password": "802545ba4ec603304d87afa3d818bf9a", "username": "2524726232@qq.com"}
    token=login.token(login_dict)
    json_dict_class={"id":None,"name":"auto_ht_classify","enabled":"ENABLED","type":[],"seq":1,"specJson":[],"pid":0,"lvl":1}
    json_dict_class_updata={"id":"1654699182384451586","name":"auto_测试","spuIds":[],"treeIds":"","lvl":1,"metaKey":"","metaTitle":"","metaDesc":"","staticUrl":"auto_test"}
    a=json_dict_class_updata.get('name')
    id='1658053324301627394'
    resp=test.goods_ht_delete_classify("auto_ht_classify1",token)


